﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace FruitMatching.DataModel
{
    public static class XmlHelper
    {
        public static List<Question> GetAllQuestions(string uri)
        {
            List<Question> list = new List<Question>();
            using (XmlReader reader = XmlReader.Create(uri))
            {
                string imgFruitParternSrc = null;
                string imgAnswer1Src = null;
                string imgAnswer2Src = null;
                string imgAnswer3Src = null;
                string imgAnswer4Src = null;
                string rightAnswer = null;

                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element
                        && reader.Name == "Question")
                    {
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "imgFruitParternSrc")
                            {
                                imgFruitParternSrc = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "imgAnswer1Src")
                            {
                                imgAnswer1Src = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "imgAnswer2Src")
                            {
                                imgAnswer2Src = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "imgAnswer3Src")
                            {
                                imgAnswer3Src = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "imgAnswer4Src")
                            {
                                imgAnswer4Src = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element &&
                                reader.Name == "rightAnswer")
                            {
                                rightAnswer = (String)reader.ReadElementContentAs(typeof(System.String), null);
                                break;
                            }
                        }
                       list.Add(new Question(imgFruitParternSrc, imgAnswer1Src, imgAnswer2Src, 
                           imgAnswer3Src, imgAnswer4Src, rightAnswer));
                    }
                }
                return list;
            }
        }
    }
}
